package com.wanghua.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.*;

import com.wanghua.mall.product.entity.CategoryEntity;
import com.wanghua.mall.product.service.CategoryService;
import com.wanghua.common.utils.PageUtils;
import com.wanghua.common.utils.R;

import javax.annotation.Resource;


/**
 * 商品三级分类
 *
 * @author wanghua
 * @email 2367279431@qq.com
 * @date 2021-08-23 16:10:06
 */
@RestController
@RequestMapping("/product/category")
public class CategoryController {

    @Resource
    private CategoryService categoryService;

    /**
     * 批量删除没有子菜单的菜单，且菜单没有被其他地方引用
     */
    @DeleteMapping("/deleteBatch")
    public R deleteBatch(@RequestBody Long[] ids) {
        categoryService.deleteBatch(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 查出所有分类及其子分类，以树形结构组织
     */
    @GetMapping("/list/tree")
    public R listWithTree() {
        List<CategoryEntity> tree = categoryService.listWithTree();
        return R.ok().put("items", tree);
    }

    /**
     * 拖拽后的修改
     */
    @PutMapping("/update/sort")
    public R updateSort(@RequestBody CategoryEntity[] categoryEntities) {
        categoryService.updateBatchById(Arrays.asList(categoryEntities));
        return R.ok();
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryEntity category = categoryService.getById(id);
        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);
        return R.ok();
    }

    /**
     * 修改，更新品牌分类关系表
     */
    @PutMapping("/update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateDetail(category);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Long[] catIds) {
        categoryService.removeByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
